<template>
  <div class="tenantMenu_main main_container">
    <div class="main_header">
      <div class="padding_container">
        <div class="left" style="color: #333333;padding-left: 10px">租户管理</div>
        <div class="right">
          <el-popover
            placement="bottom"
            popper-class="tenantManage_batchOperation"
            trigger="hover"
            :visible-arrow="false"
          >
            <div class="batchOperation_item">
              <div class="item">
                <a class="right_button" @click="updateWX">
                  <svg-icon icon-class="微信" />
                  更新微信用户
                </a>
              </div>
              <div class="item">
                <a class="right_button" @click.stop="initFileSystem">
                  <svg-icon icon-class="system" />
                  <div class="button_item_padding_name">初始化文件服务</div>
                </a>
              </div>

            </div>
            <a slot="reference" class="button_item">
              <div class="button_item_padding">
                <svg-icon icon-class="system" />
                系统快捷操作
              </div>
            </a>
          </el-popover>
          <HeaderOperation :parent="this" />
        </div>
      </div>
    </div>
    <div class="main_main">
      <SearchBar :parent="this" />
      <div ref="table_container" class="table_container">
        <el-table
          v-if="tableColumns.length>0"
          ref="table"
          :data="tableData"
          stripe
          border
          :height="tableHeight"
          style="width: 99.9%"
          @sort-change="(column)=>{sortChange(column, this)}"
          @selection-change="(val)=>{selectionChange(val, this)}"
        >
          <Column v-for="(column,index) in tableColumns" :key="index" :prop="column.prop" :is-show="column.isShow" :label="column.label" :sortable="column.sortable" :width="column.width" :min-width="column.minWidth" />
          <el-table-column label="操作" align="center" fixed="right" width="180">
            <template slot-scope="scope">
              <div class="buttom_right">
                <el-button
                  type="text"
                  @click.stop="edit(scope.row)"
                >编辑</el-button>
                <el-button
                  type="text"
                  @click.stop="deleteOne(scope.row)"
                >删除</el-button>
                <el-button
                  type="text"
                  @click.stop="printConfiguration(scope.row)"
                >打印配置</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="main_bottom">
      <pagination
        :total="total"
        :page.sync="pageData.pageNum"
        :limit.sync="pageData.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { list, remove, connetWx, initFileSystem } from '@/api/superManage/tenantManage'
import AddOrUpdate from './addOrUpdate'
import EditPrintConfiguration from './editPrintConfiguration.vue'
import { listAll } from '@/api/superManage/tenantRole'
export default {
  name: 'Index',
  data() {
    return {
      pageId: '租户管理',
      pageData: {
        roleId: undefined,
        registerRealName: undefined,
        companyName: undefined,
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'updateTime',
        isAsc: 'desc'
      },
      total: 1, // 数据一共有多少条
      mainId: 'id',
      tableHeight: '0',
      tableData: [],

      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      defaultTableColumns: [// 表格常规字段（初始值）
        { prop: 'useStatus', label: '状态', minWidth: '80' },
        { prop: 'name', label: '企业名称', minWidth: '200' },
        { prop: 'code', label: '企业编码', minWidth: '200' },
        { prop: 'buttSales', label: '所属销售', minWidth: '100' },
        { prop: 'rolesNames', label: '租户角色', minWidth: '100' },
        { prop: 'registerName', label: '注册人', minWidth: '100' },
        { prop: 'registerPhone', label: '注册手机号', minWidth: '150' },
        { prop: 'createTime', label: '注册时间', minWidth: '180' },
        { prop: 'typeName', label: '账号类型', minWidth: '100' },
        { prop: 'secondUserNums', label: '二级用户数', minWidth: '100' },
        { prop: 'foreverUseStatusName', label: '永久使用', minWidth: '80' },
        { prop: 'leftDays', label: '剩余天数', minWidth: '80' },
        { prop: 'expirationTime', label: '到期时间', minWidth: '180' },
        { prop: 'contacts', label: '企业联系人', minWidth: '80' },
        { prop: 'contactsWays', label: '联系方式', minWidth: '150' },
        { prop: 'email', label: '联系邮箱', minWidth: '150' },
        { prop: 'address', label: '企业地址', minWidth: '200' },
        { prop: 'remark', label: '备注', minWidth: '200' }
      ],
      defaultSearchColumns: [// 搜索区常规字段（初始值）
        { prop: 'name', label: '企业名称', type: 'input' },
        { prop: 'code', label: '企业编码', type: 'input' },
        { prop: 'buttSales', label: '所属销售', type: 'input' },
        { prop: 'registerName', label: '注册人', type: 'input' },
        { prop: 'registerPhone', label: '注册手机号', type: 'input' },
        { prop: 'contacts', label: '企业联系人', type: 'input' },
        { prop: 'contactsWays', label: '联系方式', type: 'input' },
        { prop: 'email', label: '联系邮箱', type: 'input' },
        { prop: 'address', label: '企业地址', type: 'input' },
        { prop: 'rolesNames', label: '租户角色', type: 'input' },
        { prop: 'useStatus', label: '状态', type: 'select', optionsName: 'statusOptions' },
        { prop: 'typeName', label: '账号类型', type: 'select', optionsName: 'typeOptions' },
        { prop: 'secondUserNums', label: '二级用户数', type: 'select', optionsName: 'roleOptions' },
        { prop: 'foreverUseStatus', label: '永久使用', type: 'select', optionsName: 'employOptions' },
        { prop: 'createTime', label: '注册时间', type: 'time' },
        { prop: 'expirationTime', label: '到期时间', type: 'time' }
      ],
      operationData: [ // 右上角的操作按钮
        // { label: '新增', perm: '' },
        { label: '页面配置', perm: '' }
      ],
      roleOptions: [
        { value: 15, label: '15' },
        { value: 50, label: '50' },
        { value: -1, label: '不限' }
      ],
      statusOptions: [
        { value: undefined, label: '全部' },
        { value: '使用中', label: '使用中' },
        { value: '已停用', label: '已停用' },
        { value: '已到期', label: '已到期' },
        { value: '即将到期', label: '即将到期' }
      ],
      employOptions: [
        { value: undefined, label: '全部' },
        { value: '1', label: '是' },
        { value: '2', label: '否' }
      ],
      typeOptions: [
        { value: undefined, label: '全部' },
        { value: '1', label: '试用版' },
        { value: '2', label: '正式版' }
      ]
    }
  },
  computed: {
    selectedNum() {
      return this.$refs.table ? this.$refs.table.selection.length : 0
    }
  },
  methods: {
    printConfiguration(row) {
      this.$layer.iframe({// 弹框
        content: {
          content: EditPrintConfiguration, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            tenantId: row.id
          }// props
        },
        area: ['800px', '700px'],
        title: '打印配置', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    getList() {
      list(this.pageData).then(res => {
        this.tableData = res.data
        this.tableData.forEach(ele => {
          if (ele.secondUserNums == '-1') {
            ele.secondUserNums = '不限'
          }
          if (ele.foreverUseStatus == 1) {
            ele.leftDays = '永久'
          }
        })
        this.total = res.total
        this.$nextTick(() => {
          this.setSelectRow(this)// 设置行选中
          this.tableHeight = this.$refs.table_container.offsetHeight
        })
      })
    },
    refresh() {
      this.pageData.pageNum = 1
      this.getList()
    },
    updateWX() {
      connetWx().then(res => {
        this.$message({
          message: res.msg,
          type: 'success'
        })
      })
    },
    initFileSystem() {
      initFileSystem().then(res => {
        this.$message({
          message: res.msg,
          type: 'success'
        })
      })
    },
    edit(row) {
      this.$layer.iframe({// 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            id: row[this.mainId]
          }// props
        },
        area: ['1320px', '760px'],
        title: '编辑租户', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    deleteOne(row) {
      const _this = this
      this.$confirm('确定要删除吗', '删除确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消', cancelButtonClass: 'cancelConfirm',
        type: 'warning'
      }).then(function() {
        remove({ deleteIds: row[_this.mainId] }).then(res => {
          _this.$message({
            message: res.msg,
            type: 'success'
          })
          _this.getList()
        })
      }).catch(() => {})
    }
  }
}
</script>

<style lang="less" scoped>
.buttom_right {
  display: flex;
}

.right_button{
  color: #333333;
  padding: 0px 15px;
  height: 30px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 4px;
  font-size: 14px;
  /deep/ .svg-icon{
    margin-right: 10px;
  }
}
.right_button:hover{
  background-color: rgba(56,123,252,.5);
  color: #fff;
}
.tenantManage_batchOperation{
  width: 200px;
}
</style>
